package me.zhengjie.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.With;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.WcsReport.BuzzerService;
import me.zhengjie.WcsReport.WcsReportService;
import me.zhengjie.agvSend.AgvSendService;
import me.zhengjie.pojo.dto.*;
import me.zhengjie.pojo.entity.*;
import me.zhengjie.pojo.vo.*;
import me.zhengjie.sqlservice.*;
import me.zhengjie.sqlservice.impl.IwsSOutsideServiceImpl;
import me.zhengjie.tcp.FluentFrame;
import me.zhengjie.utility.IdWorker;
import me.zhengjie.utility.state.MaterialStatus;
import me.zhengjie.utility.state.TaskStatus;
import me.zhengjie.utility.state.TaskType;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
@Slf4j
@Api
public class TestWms {
    private final WcsReportService wcsReportService;

    private final WcsAgvIdService wcsAgvIdService;

    private final AgvSendService agvSendService;

    private final NestingMiddleTableService nestingMiddleTableService;

    private final IdWorker idWorker;

    private final WcsInboundMissionService wcsInboundMissionService;

    private final BuzzerService buzzerService;


    private final IwsStoreOutsideService iwsStoreOutsideService;

    private final WcsThreeFloorOutboundService wcsThreeFloorOutboundService;

    private final WcsFluentFrameConfigService wcsFluentFrameConfigService;

    private final FluentFrame fluentFrame;


//    private final MapToBeanCopier mapToBeanCopier;

    @GetMapping("/isCodeExist/{containerCode}")
    @ApiOperation("入库查询容器是否已入库")
    //完成
    public boolean test1(@PathVariable String containerCode){
        InboundIsCodeExistDto inboundIsCodeExistDto = new InboundIsCodeExistDto();
        inboundIsCodeExistDto.setContainerCode(containerCode);
        inboundIsCodeExistDto.setNest(false);
        inboundIsCodeExistDto.setFloor(TaskType.TWO_FLOOR.getMessage());
        boolean b = wcsReportService.inboundIsCodeExist(inboundIsCodeExistDto);
        return b;
    }

    @GetMapping("/{workOrder}")
    @ApiOperation("入库查询容器是否已入库")
    //完成
    public boolean workOrder(@PathVariable String workOrder){
        wcsThreeFloorOutboundService.updateWmsStatus(workOrder,1);
        boolean b = wcsReportService.wmsThreeFloorWorkOrderComplete(workOrder);
        return b;
    }

    @GetMapping("/inboundStorageApply/{containerCode}")
    @ApiOperation("入库储位申请创建入库任务")
    //完成
    public String test2(@PathVariable String containerCode){
        InboundIsCodeExistDto inboundIsCodeExistDto = new InboundIsCodeExistDto();
        inboundIsCodeExistDto.setContainerCode(containerCode);
        inboundIsCodeExistDto.setNest(false);
        boolean b = wcsReportService.inboundIsCodeExist(inboundIsCodeExistDto);
        if (b){
            return  "该容器已存在库区";
        }


        List<NestingMiddleTable> nestingMiddleTables = nestingMiddleTableService.selectContainerCode(containerCode);
        InboundStorageApplyDto1 inboundStorageApplyDto = new InboundStorageApplyDto1();
        inboundStorageApplyDto.setContainerCode(containerCode);
        inboundStorageApplyDto.setFloor(TaskType.ONE_FLOOR.getMessage());
        inboundStorageApplyDto.setNest(false);
        ArrayList<NestingMiddleTable> materialDaos = new ArrayList<>();
        for (NestingMiddleTable nestingMiddleTable : nestingMiddleTables) {
            NestingMiddleTable nestingMiddleTable1 = new NestingMiddleTable();
            BeanUtils.copyProperties(nestingMiddleTable,nestingMiddleTable1);
            materialDaos.add(nestingMiddleTable1);
        }
        inboundStorageApplyDto.setData(materialDaos);
        String destination = wcsReportService.inboundStorageApply(inboundStorageApplyDto);
        log.info("入库申请终点储位：{}",destination);
        if (destination != null) {
            NestingMiddleTable nestingMiddleTable = nestingMiddleTables.get(0);
            WcsInboundMission wcsInboundMission = new WcsInboundMission();
            String inboundCode = String.valueOf(idWorker.nextId());
            wcsInboundMission.setInboundCode(inboundCode);
            wcsInboundMission.setMissionStatus(TaskStatus.Task_create.getStatus());
            wcsInboundMission.setContainerCode(containerCode);
            wcsInboundMission.setSource("起点");
            wcsInboundMission.setDestination(destination);
            wcsInboundMission.setContainerType(TaskType.PALLET.getStatus());
            wcsInboundMission.setCreateTime(LocalDateTime.now());
            wcsInboundMission.setUpdateTime(LocalDateTime.now());
            wcsInboundMission.setInboundType(TaskType.inbound_ordinary.getStatus());
            wcsInboundMission.setIsNest(0);
            wcsInboundMission.setIsPlc(1);
            StringBuilder sb = new StringBuilder();
            List<String> materialCodeList = nestingMiddleTables.stream().map(NestingMiddleTable::getMaterialCode).distinct().collect(Collectors.toList());
            for (String materialCode : materialCodeList) {
                if (sb.length() >0){
                    sb.append(",");
                }
                sb.append(materialCode);
            }
            String materIalCode = sb.toString();
            wcsInboundMission.setMaterialCode(materIalCode);
            boolean b1 = wcsInboundMissionService.insertInbound(wcsInboundMission);
            if (!b1) {
                log.info("创建入库任务失败");
                throw new RuntimeException("创建入库任务失败");
            }
            return destination;
        }
        return "终点储位为null";
    }

    @GetMapping("/pickPlaceReport/{type}")
    //1
    public boolean test3(@PathVariable Integer type){
        takeReportDto takeReportDto = new takeReportDto();
        takeReportDto.setContainerCode("测试容器编码");
        takeReportDto.setStorageCode("储位编码");
        takeReportDto.setType(type);
        takeReportDto.setTaskType("type");
        takeReportDto.setNest(true);
        boolean b = wcsReportService.pickPlaceReport(takeReportDto);
        return b;
    }

    @GetMapping("/circleLibraryObtainPalletCode")
    public ReturnVo test4(){
        ReturnVo returnVo = wcsReportService.circleLibraryObtainPalletCode("palletCode");
        return returnVo;
    }

    @GetMapping("/emptyContainer")
    public ReturnVo test5(){
        ReturnVo returnVo = wcsReportService.emptyContainer("储位编码");
        EmptyContainer emptyContainer = new ObjectMapper().convertValue(returnVo.getData(), EmptyContainer.class);
        System.out.println("空箱出库："+emptyContainer);
        return returnVo;
    }




    @GetMapping("/wmsInventoryInformation")
    //完成
    public TestVo test6() throws Exception {
        TestVo testVo = wcsReportService.wmsInventoryInformation("C00099");
        log.info("打印：{}", testVo);
        List<NestingMiddleTable> nestingMiddleTableList = new ArrayList<>();
        NestingMiddleTable nestingMiddleTable = new NestingMiddleTable();
        nestingMiddleTable.setItemBarCode("ITEM00003471");
        nestingMiddleTable.setInventoryQuantity(20.118383433666565);
        NestingMiddleTable nestingMiddleTable1 = new NestingMiddleTable();
        nestingMiddleTable1.setItemBarCode("ITEM00003561");
        nestingMiddleTable1.setInventoryQuantity(325.8826889625625);
        nestingMiddleTableList.add(nestingMiddleTable);
        nestingMiddleTableList.add(nestingMiddleTable1);
        List<MaterialDao> data = testVo.getData();
        HashSet<InventoryContrast> mesInventorySet = new HashSet<>();
        HashSet<InventoryContrast> wmsInventorySet = new HashSet<>();
//        BeanUtils.copyProperties(nestingMiddleTableList,mesInventorySet);
//        BeanUtils.copyProperties(data,wmsInventorySet);
        for (MaterialDao datum : data) {
            InventoryContrast wmsInventory = new InventoryContrast();
            BeanUtils.copyProperties(datum,wmsInventory);
            wmsInventorySet.add(wmsInventory);
        }
        for (NestingMiddleTable middleTable : nestingMiddleTableList) {
            InventoryContrast wmsInventory = new InventoryContrast();
            BeanUtils.copyProperties(middleTable,wmsInventory);
            mesInventorySet.add(wmsInventory);
        }
        if (mesInventorySet.equals(wmsInventorySet)){
            log.info("一致");
        }else {
            log.info("不一致，wms数据：{},wcs:{}",wmsInventorySet,mesInventorySet);
        }

        return null;
    }

    @GetMapping("/cancelInboundStorage/{containerCode}")
    @ApiOperation("入库任务取消")
    //完成
    public boolean test7(@PathVariable String containerCode){
        List<WcsInboundMission> wcsInboundMissionList = wcsInboundMissionService.selectContainerCode(containerCode);
        if (!wcsInboundMissionList.isEmpty()){
            WcsInboundMission wcsInboundMission = wcsInboundMissionList.get(0);
            CancelInboundStorageDto cancelInboundStorageDto = new CancelInboundStorageDto();
            cancelInboundStorageDto.setStorageId(wcsInboundMission.getDestination());
            cancelInboundStorageDto.setContainerCode(wcsInboundMission.getContainerCode());
            Integer isNest = wcsInboundMission.getIsNest();
            if (isNest ==1){
                cancelInboundStorageDto.setNest(true);
            }else {
                cancelInboundStorageDto.setNest(false);
            }
            boolean b = wcsReportService.cancelInboundStorage(cancelInboundStorageDto);
            if (b){
                //刪除任務
                wcsInboundMissionService.updateMissionStatus(wcsInboundMission.getInboundCode(), 5);
            }
            return b;
        }
        return false;
    }

    //任务下发 点对点
//    {
//        "taskType" : "PF-FMR-COMMON",
//            "robotTaskCode" : "1372488009701789605",
//            "initPriority" : 99,
//            "targetRoute" : [ {
//        "seq" : 0,
//                "type" : "SITE",
//                "code" : "0531430AA0504935"
//    }, {
//        "seq" : 1,
//                "type" : "SITE",
//                "code" : "0536425AA0504935"
//    } ]
//    }

    //任务下发启点 中间点 终点
//    {
//      "taskType" : "PF-FMR-TASK-CONTINUE",
//      "robotTaskCode" : "1372488009701789691",
//      "initPriority" : 99,
//      "targetRoute" : [ {
//        "seq" : 0,
//        "type" : "SITE",
//        "code" : "0525755AA0509250"
//      }, {
//        "seq" : 1,
//        "type" : "SITE",
//        "code" : "0523569AA0509300"
//      } ]
//    }

    //链条机 0523569AA0509300 0521507AA0509490
    @PostMapping("/agvTaskSubmit")
    public String test8(@RequestBody AgvTaskSubmitDto agvTaskSubmitDto){
        String s = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
        return s;
    }

    //{
    //  "triggerCode" : "A02",
    //  "robotTaskCode" : "125465fd",
    //  "triggerType" : "SITE",
    //  "targetRoute" : {
    //    "code" : "A02",
    //    "type" : "SITE",
    //    "seq" : 1
    //  }
    //}
    //任务继续执行
    @PostMapping("/agvTaskContinueExecuteDtof")
    public boolean test9(@RequestBody AGVTaskContinueExecuteDto agvTaskContinueExecuteDto){
        boolean b = agvSendService.taskContinueExecute(agvTaskContinueExecuteDto);
        return b;
    }

    //{
    //  "siteCode" : "lift01",
    //  "taskCode" : "124353",
    //  "actionStatus" : "rtew",
    //  "eqptCode" : "ytr"
    //}
    //外设执行通知接口 1楼电梯电梯点位0540010AA0506055 2楼电梯点位0500000BB0500000电梯点  0500000BB0501590 2楼出电梯点 0502700BB0501590
    @PostMapping("/noticeAgv")
    public String test10(@RequestBody NoticeAgvDto noticeAgvDto){
        boolean b = agvSendService.noticeAgv(noticeAgvDto);
        if (b){
            return "成功";
        }
        return "失败";
    }


    //任务取消
    @PostMapping("/taskCancel")
    public AgvTaskSubmitVo taskCancel(@RequestBody TaskCancelDto taskCancelDto){
        AgvTaskSubmitVo agvTaskSubmitVo = agvSendService.taskCancel(taskCancelDto);
        return agvTaskSubmitVo;
    }

    //储位绑定栈板 0531430AA0504935   0521507AA0509490
    @PostMapping("/carrierUnbind")
    public AgvTaskSubmitVo carrierUnbind(@RequestBody CarrierUnbindDto  carrierUnbindDto){
        AgvTaskSubmitVo agvTaskSubmitVo = agvSendService.carrierUnbind(carrierUnbindDto);
        return agvTaskSubmitVo;
    }

    //鼎升AGV点对点任务先绑定栈板储位
    @PostMapping("/dingsheng")
    public AgvTaskSubmitVo dingsheng(@RequestBody DingshengDto dingshengDto){
        AgvTaskSubmitVo agvTaskSubmitVo = agvSendService.dingsheng(dingshengDto);
        return agvTaskSubmitVo;
    }

    //鼎升AGV点对点任务先绑定栈板储位
    @GetMapping("/buzzer/{red}/{buzzer}")
    public String buzzer(@PathVariable Integer red,@PathVariable Integer buzzer){
        BuzzerDto buzzerDto = new BuzzerDto();
        buzzerDto.setSite(1);
        buzzerDto.setRed(red);
        buzzerDto.setBuzzer(buzzer);
        BuzzerVo buzzerVo = buzzerService.buzzerSend(buzzerDto,"http://192.168.9.20:9999");
        return "1";
    }

    //单片机
    @GetMapping("/mcu/{count}")
    public String mcu(@PathVariable Integer count){
        McuDto mcuDto = new McuDto();
        ArrayList<McuCount> mcuCounts = new ArrayList<>();
        McuCount mcuCount = new McuCount();
        mcuCount.setCount(count);
        mcuCounts.add(mcuCount);
        mcuDto.setDetails(mcuCounts);
        McuVo mcuVo = buzzerService.mcuSend(mcuDto, "http://192.168.9.20:5000");
        return "1";
    }



//    {
//        "siteCode" : "R3FchuA01011",
//            "nextTaskTime" : "20",
//            "robotType" : "17"
//    }
    @PostMapping("/task/Pretask")
    public void testBox(@RequestBody TaskPretaskDto  taskPretaskDto){
        AgvTaskSubmitVo agvTaskSubmitVo = agvSendService.taskPretask(taskPretaskDto);
    }

    @GetMapping("/test/task/pretask/{carrierCode}")
    public Void testTaskPretask(@PathVariable String carrierCode){
//        TaskPretaskDto taskPretaskDto = new TaskPretaskDto();
//        taskPretaskDto.setSiteCode("R3FchuA01011");
//        taskPretaskDto.setNextTaskTime("20");
//        taskPretaskDto.setRobotType("17");
//        AgvTaskSubmitVo agvTaskSubmitVo = agvSendService.taskPretask(taskPretaskDto);

        long l = idWorker.nextId();
        String inboundCode = String.valueOf(l);
        //创建3楼入库任务
        WcsInboundMission wcsInboundMission = new WcsInboundMission();
        wcsInboundMission.setInboundCode(inboundCode);
        wcsInboundMission.setMissionStatus(TaskStatus.Task_create.getStatus());
        wcsInboundMission.setContainerCode(carrierCode);
        wcsInboundMission.setSource("R3FchuA01011");
        wcsInboundMission.setDestination("AA");
        wcsInboundMission.setContainerType(TaskType.THREE_WORKBIN.getStatus());
        wcsInboundMission.setCreateTime(LocalDateTime.now());
        wcsInboundMission.setUpdateTime(LocalDateTime.now());
        wcsInboundMission.setInboundType(TaskType.inbound_ordinary.getStatus());
        wcsInboundMission.setPriority(10);
        wcsInboundMission.setIsNest(0);
        wcsInboundMission.setIsPlc(0);
        wcsInboundMission.setIsWms(0);
        //创建入库任务
        boolean b2 = wcsInboundMissionService.insertInbound(wcsInboundMission);
        //任务下发
        AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
        agvTaskSubmitDto.setTaskType("PF-CTU-RUKU");
        ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
        TargetRoute targetRoute = new TargetRoute();
        TargetRoute targetRoutes = new TargetRoute();
        //添加起点
        targetRoute.setSeq(0);
        targetRoute.setType(TaskType.STORAGE.getMessage());
        targetRoute.setCode("R3FchuA01011");
        CarrierInfoDto carrierInfoDto = new CarrierInfoDto();
        carrierInfoDto.setCarrierCode(carrierCode);
        carrierInfoDto.setCarrierType("1");
        ArrayList<CarrierInfoDto> carrierInfoDtos = new ArrayList<>();
        carrierInfoDtos.add(carrierInfoDto);
        ExtraDto extraDto = new ExtraDto();
        extraDto.setCarrierInfo(carrierInfoDtos);
//        targetRoute.setExtra(extraDto);
        //添加终点
        targetRoutes.setSeq(1);
        targetRoutes.setType("ZONE");
        targetRoutes.setCode("AA");
        targetRouteList.add(targetRoute);
        targetRouteList.add(targetRoutes);
        agvTaskSubmitDto.setTargetRoute(targetRouteList);
        agvTaskSubmitDto.setRobotTaskCode(inboundCode);
        agvTaskSubmitDto.setInitPriority(10);
        agvTaskSubmitDto.setExtra(extraDto);
        String s = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
        return null;
    }

    @GetMapping("/wms/test/inbound/{containerCode}")
    public void floorWmsTest(@PathVariable String containerCode){
        InboundStorageApplyDto1 inboundStorageApplyDto1 = new InboundStorageApplyDto1();
        inboundStorageApplyDto1.setFloor("M3");
        inboundStorageApplyDto1.setContainerCode(containerCode);
        inboundStorageApplyDto1.setNest(false);
        List<NestingMiddleTable> nestingMiddleTables = nestingMiddleTableService.selectContainerCode(containerCode);
        ArrayList<NestingMiddleTable> materialDaos = new ArrayList<>();
        for (NestingMiddleTable nestingMiddleTable : nestingMiddleTables) {
            NestingMiddleTable nestingMiddleTable1 = new NestingMiddleTable();
            BeanUtils.copyProperties(nestingMiddleTable,nestingMiddleTable1);
            materialDaos.add(nestingMiddleTable1);
        }
        inboundStorageApplyDto1.setData(materialDaos);
        //向wms发送起点储位申请
        String s = wcsReportService.inboundStorageApply(inboundStorageApplyDto1);
        System.out.println("终点储位分配："+s);
        if (s !=null){
            //获取终点储位，创建任务，下发AGV任务
            String inboundCode = String.valueOf(idWorker.nextId());
            WcsInboundMission wcsInboundMission = new WcsInboundMission();
            wcsInboundMission.setInboundCode(inboundCode);
            wcsInboundMission.setMissionStatus(TaskStatus.Task_create.getStatus());
            wcsInboundMission.setContainerCode(containerCode);
            wcsInboundMission.setSource("R3FchuA01011");
            wcsInboundMission.setDestination(s);
            wcsInboundMission.setFloor("M3");
            wcsInboundMission.setContainerType(TaskType.THREE_WORKBIN.getStatus());
            wcsInboundMission.setCreateTime(LocalDateTime.now());
            wcsInboundMission.setUpdateTime(LocalDateTime.now());
            wcsInboundMission.setInboundType(TaskType.inbound_ordinary.getStatus());
            wcsInboundMission.setPriority(10);
            wcsInboundMission.setIsNest(0);
            wcsInboundMission.setIsPlc(0);
            wcsInboundMission.setIsWms(1);
            StringBuilder sb = new StringBuilder();
            List<String> materialCodeList = nestingMiddleTables.stream().map(NestingMiddleTable::getMaterialCode).distinct().collect(Collectors.toList());
            for (String materialCode : materialCodeList) {
                if (sb.length() > 0) {
                    sb.append(",");
                }
                sb.append(materialCode);
            }
            String materIalCode = sb.toString();
            wcsInboundMission.setMaterialCode(materIalCode);
            //创建入库任务
            boolean b2 = wcsInboundMissionService.insertInbound(wcsInboundMission);

            //任务下发
            AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
            agvTaskSubmitDto.setTaskType("PF-CTU-RUKU");
            ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
            TargetRoute targetRoute = new TargetRoute();
            TargetRoute targetRoutes = new TargetRoute();
            //添加起点
            targetRoute.setSeq(0);
            targetRoute.setType(TaskType.STORAGE.getMessage());
            targetRoute.setCode("R3FchuA01011");
            CarrierInfoDto carrierInfoDto = new CarrierInfoDto();
            carrierInfoDto.setCarrierCode(containerCode);
            carrierInfoDto.setCarrierType("1");
            ArrayList<CarrierInfoDto> carrierInfoDtos = new ArrayList<>();
            carrierInfoDtos.add(carrierInfoDto);
            ExtraDto extraDto = new ExtraDto();
            extraDto.setCarrierInfo(carrierInfoDtos);
//        targetRoute.setExtra(extraDto);
            //添加终点
            targetRoutes.setSeq(1);
            targetRoutes.setType(TaskType.STORAGE.getMessage());
            targetRoutes.setCode(s);
            targetRouteList.add(targetRoute);
            targetRouteList.add(targetRoutes);
            agvTaskSubmitDto.setTargetRoute(targetRouteList);
            agvTaskSubmitDto.setRobotTaskCode(inboundCode);
            agvTaskSubmitDto.setInitPriority(10);
            agvTaskSubmitDto.setExtra(extraDto);
            agvSendService.agvTaskSubmit(agvTaskSubmitDto);
        }
    }

    @PostMapping("/test/pause")
    public AgvTaskSubmitVo zonePause(@RequestBody ZonePauseDto zonePauseDto){
        AgvTaskSubmitVo agvTaskSubmitVo = agvSendService.zonePause(zonePauseDto);
        return agvTaskSubmitVo;
    }



    @PostMapping("/test0001")
    public ReturnVo test121(@RequestBody ThreeFloorDeliveryProductionLineDto threeFloorDeliveryProductionLineDto){
        boolean b = wcsReportService.wmsThreeFloorDeliveryProductionLine(threeFloorDeliveryProductionLineDto);
        if (b){
            return ReturnVo.OK();
        }
        return ReturnVo.ng("失败");
    }

    @GetMapping("test011")
    public void test01(){
        List<NestingMiddleTable> nestingMiddleTables = nestingMiddleTableService.selectContainerCode("BOX-020640");
        InboundStorageApplyDto1 inboundStorageApplyDto = new InboundStorageApplyDto1();
        inboundStorageApplyDto.setContainerCode("BOX-007023");
        inboundStorageApplyDto.setNest(false);
        inboundStorageApplyDto.setFloor(TaskType.THREE_FLOOR.getMessage());
        ArrayList<NestingMiddleTable> materialDaos = new ArrayList<>();
        for (NestingMiddleTable nestingMiddleTable : nestingMiddleTables) {
            NestingMiddleTable nestingMiddleTable1 = new NestingMiddleTable();
            BeanUtils.copyProperties(nestingMiddleTable, nestingMiddleTable1);
            materialDaos.add(nestingMiddleTable1);
        }
        inboundStorageApplyDto.setData(materialDaos);
        String destination = wcsReportService.inboundStorageApply(inboundStorageApplyDto);
    }

    //下发agv失败，重新下发
    @GetMapping("outbound/again/issue")
    public ReturnVo againIssue(){
        ReturnVo returnVo = wcsReportService.againIssue();
        return returnVo;
    }


    //下发agv失败，重新下发
    @PostMapping("outbound/delivery/productionLine")
    public ReturnVo outboundDeliveryProductionLine(@RequestBody List<String> workOrderList){
        ReturnVo returnVo = wcsReportService.outboundDeliveryProductionLine(workOrderList);
        return returnVo;
    }

    //下发agv失败，重新下发
    @GetMapping("plc/test")
    public void plcTest(){
            List<WcsFluentFrameConfig> wcsFluentFrameConfigs = wcsFluentFrameConfigService.selectStorageRegion("192.168.48.21");
        long start = System.nanoTime();
        for (int i = 0; i < 10000; i++) {
            for (WcsFluentFrameConfig wcsFluentFrameConfig : wcsFluentFrameConfigs) {
                String region = wcsFluentFrameConfig.getRegion();
                Integer unitId = wcsFluentFrameConfig.getUnitId();
                Integer cylinder = wcsFluentFrameConfig.getCylinder();
                Integer outboundOneAddress = wcsFluentFrameConfig.getOutboundOneAddress();
                Integer outboundTwoAddress = wcsFluentFrameConfig.getOutboundTwoAddress();
                Integer outboundThreeAddress = wcsFluentFrameConfig.getOutboundThreeAddress();
                Integer sortingAddress = wcsFluentFrameConfig.getSortingAddress();
                Integer inboundOneAddress = wcsFluentFrameConfig.getInboundOneAddress();
                Integer inboundTwoAddress = wcsFluentFrameConfig.getInboundTwoAddress();
                Integer inboundThreeAddress = wcsFluentFrameConfig.getInboundThreeAddress();
                Integer sortingReadAddress = wcsFluentFrameConfig.getSortingReadAddress();
                Integer sortingResultAddress = wcsFluentFrameConfig.getSortingResultAddress();
                Integer outboundStorageSizeAddress = wcsFluentFrameConfig.getOutboundStorageSizeAddress();
                Integer cylinderControlAddress = wcsFluentFrameConfig.getCylinderControlAddress();
                Integer oneInbReadAddress = wcsFluentFrameConfig.getOneInbReadAddress();
                Integer twoInbReadAddress = wcsFluentFrameConfig.getTwoInbReadAddress();
                Integer threeInbReadAddress = wcsFluentFrameConfig.getThreeInbReadAddress();
                Integer integer = wcsFluentFrameConfig.getInbRequestTake();
                Integer outRequestRelease = wcsFluentFrameConfig.getOutRequestRelease();
                Integer integer1 = fluentFrame.readHoldingRegisters(region, cylinder, unitId);
                Integer integer2 = fluentFrame.readHoldingRegisters(region, outboundOneAddress, unitId);
                Integer integer3 = fluentFrame.readHoldingRegisters(region, outboundTwoAddress, unitId);
                Integer integer4 = fluentFrame.readHoldingRegisters(region, outboundThreeAddress, unitId);
                Integer integer5 = fluentFrame.readHoldingRegisters(region, sortingAddress, unitId);
                Integer integer6 = fluentFrame.readHoldingRegisters(region, inboundOneAddress, unitId);
                Integer integer7 = fluentFrame.readHoldingRegisters(region, inboundTwoAddress, unitId);
                Integer integer8 = fluentFrame.readHoldingRegisters(region, inboundThreeAddress, unitId);
                Integer integer9 = fluentFrame.readHoldingRegisters(region, sortingReadAddress, unitId);
                Integer integer10 = fluentFrame.readHoldingRegisters(region, sortingResultAddress, unitId);
                Integer integer11 = fluentFrame.readHoldingRegisters(region, outboundStorageSizeAddress, unitId);
                Integer integer12 = fluentFrame.readHoldingRegisters(region, cylinderControlAddress, unitId);
                Integer integer13 = fluentFrame.readHoldingRegisters(region, oneInbReadAddress, unitId);
                Integer integer14 = fluentFrame.readHoldingRegisters(region, twoInbReadAddress, unitId);
                Integer integer15 = fluentFrame.readHoldingRegisters(region, threeInbReadAddress, unitId);
                Integer integer16 = fluentFrame.readHoldingRegisters(region, integer, unitId);
                Integer integer17 = fluentFrame.readHoldingRegisters(region, outRequestRelease, unitId);
                System.out.println("1:"+cylinder +":"+integer1);
                System.out.println("2:"+outboundOneAddress +":"+integer2);
                System.out.println("3:"+outboundTwoAddress +":"+integer3);
                System.out.println("4:"+outboundThreeAddress +":"+integer4);
                System.out.println("5:"+sortingAddress +":"+integer5);
                System.out.println("6:"+inboundOneAddress +":"+integer6);
                System.out.println("7:"+inboundTwoAddress +":"+integer7);
                System.out.println("8:"+inboundThreeAddress +":"+integer8);
                System.out.println("9:"+sortingReadAddress +":"+integer9);
                System.out.println("10:"+sortingResultAddress +":"+integer10);
                System.out.println("11:"+outboundStorageSizeAddress +":"+integer11);
                System.out.println("12:"+cylinderControlAddress +":"+integer12);
                System.out.println("13:"+oneInbReadAddress +":"+integer13);
                System.out.println("14:"+twoInbReadAddress +":"+integer14);
                System.out.println("15:"+threeInbReadAddress +":"+integer15);
                System.out.println("16:"+integer +":"+integer16);
                System.out.println("17:"+outRequestRelease +":"+integer17);
            }
        }
        long end = System.nanoTime();
        long cost = end - start;
        double ms = cost / 1_000_000.0;
        double s  = cost / 1_000_000_000.0;
        System.out.printf("耗时: %,d ns ≈ %.3f ms ≈ %.6f s%n", cost, ms, s);
    }
}
